package zm.irc.cmd;
import java.util.HashMap;
import java.util.Map;

import com.mysql.cj.util.StringUtils;

import org.apache.log4j.Logger;

import zm.irc.client.IrcClient;
import zm.irc.ioc.annotation.AutoWired;
import zm.irc.ioc.annotation.Service;
import zm.irc.message.receive.IrcReceiveCmdMessage;
import zm.irc.message.send.IrcChatMessage;
import zm.irc.msgqueue.LocalMemoryMsgQueue;

/**
 * <pre>
 * Morse Code Translator
 * </pre>
 */
@Service(name=CommandNameConst.CMD_MORSE_CODE)
public class CmdMorseCode implements IrcChatMsgCmd{
    private static final Logger log = Logger.getLogger(CmdMorseCode.class);

    private static final Map<String,String> MORSE_MAP;
    static{
        MORSE_MAP = new HashMap<>();
        MORSE_MAP.put("A",". -");
        MORSE_MAP.put("B","- . . .");
        MORSE_MAP.put("C","- . - .");
        MORSE_MAP.put("D","- . .");
        MORSE_MAP.put("E",".");
        MORSE_MAP.put("F",". . - .");
        MORSE_MAP.put("G","- - .");
        MORSE_MAP.put("H",". . . .");
        MORSE_MAP.put("I",". .");
        MORSE_MAP.put("J",". - - -");
        MORSE_MAP.put("K","- . -");
        MORSE_MAP.put("L",". - . .");
        MORSE_MAP.put("M","- -");
        MORSE_MAP.put("N","- .");
        MORSE_MAP.put("O","- - - ");
        MORSE_MAP.put("P",". - - .");
        MORSE_MAP.put("Q","- - . -");
        MORSE_MAP.put("R",". - .");
        MORSE_MAP.put("S",". . .");
        MORSE_MAP.put("T","-");
        MORSE_MAP.put("U",". . -");
        MORSE_MAP.put("V",". . . -");
        MORSE_MAP.put("W",". - - ");
        MORSE_MAP.put("X","- . . -");
        MORSE_MAP.put("Y","- . - -");
        MORSE_MAP.put("Z","- - . .");
        MORSE_MAP.put("0","- - - - -");
        MORSE_MAP.put("1",". - - - -");
        MORSE_MAP.put("2",". . - - -");
        MORSE_MAP.put("3",". . . - -");
        MORSE_MAP.put("4",". . . . -");
        MORSE_MAP.put("5",". . . . .");
        MORSE_MAP.put("6","- . . . .");
        MORSE_MAP.put("7","- - . . .");
        MORSE_MAP.put("8","- - - . .");
        MORSE_MAP.put("9","- - - - .");
    }

    @AutoWired
    private LocalMemoryMsgQueue localMemoryMsgQueue ;


    public CmdMorseCode(){
    }


    @Override
    public boolean execute(IrcReceiveCmdMessage cmd, IrcClient client) {
        String[]  params = cmd.getCmdParams();
        if(params == null){
            log.warn("Param empty!");
            return true;
        }
        String morseMsg = "";
        try{
            String msg = String.join(" ", params);
            morseMsg =  this.stringToMorse(msg);
        }catch(Exception e){
            log.error("Morse Translate error!",e);
        }
        IrcChatMessage msg = new IrcChatMessage();
        msg.setChannel(cmd.getChannel());
        msg.setMsg(morseMsg);
        this.localMemoryMsgQueue.addSendQueue(msg);
        return true;
    }


    private String stringToMorse(String msg){
        if(StringUtils.isEmptyOrWhitespaceOnly(msg)){
            return "";
        }
        char[] msgCharArr = msg.toCharArray();
        StringBuffer morseMsg = new StringBuffer();

        for(char c : msgCharArr){
            if(c == ' '){
                continue;
            }
            String morseCode = this.charToMorse(c);
            if(StringUtils.isEmptyOrWhitespaceOnly(morseCode)){
                morseCode = "?";
            }else{
                morseCode = morseCode.replaceAll(" ", "");
            }
            
            morseMsg.append(morseCode);
            morseMsg.append(" ");
        }

        return morseMsg.toString();

    }

    private String charToMorse(Character c){
        String cStr = String.valueOf(Character.toUpperCase(c));
        return MORSE_MAP.get(cStr);
    }
    
}
